﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Infrastructure.Aspect.Extensions
{
    public static class IEnumerableExtension
    {
        //
        // Summary:
        //     Creates a System.Collections.Generic.HashSet<T> from an System.Collections.Generic.IEnumerable<T>.
        //
        // Parameters:
        //   source:
        //     The System.Collections.Generic.IEnumerable<T> to create a System.Collections.Generic.List<T>
        //     from.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     A System.Collections.Generic.List<T> that contains elements from the input
        //     sequence.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source is null.

        public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source)
        {
            HashSet<TSource> result = new HashSet<TSource>();
            foreach (var item in source)
            {
                result.Add(item);
            }
            return result;
        }
        public static int Count(this IEnumerable source)
        {
            if (source == null) return 0;
            int count = 0;
            foreach (var item in source)
            {
                count++;
            }
            return count;
        }
        public static void ExecuteAction<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
        {
            foreach (var item in source)
            {
                action(item);
            }
        }
    }
}
